package adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.common.R;

import java.util.List;

import utils.TextViewUtils;

/**
 * ltx on 2016/11/7 09:46
 * 搜索的历史记录Adapter
 */
public class HistoryAdapter extends BaseAdapter implements View.OnClickListener {
    private List<String> mHistroyList;
    private Context mContext;
    private LayoutInflater mInflater;
    private ViewHolder mViewHolder;

    private boolean isHistory;//是否是搜索历史记录

    public HistoryAdapter(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
    }

    public void setDataList(List<String> histroyList) {
        mHistroyList = histroyList;
    }
    public void setIsHistory(boolean isHistory){
        this.isHistory=isHistory;
    }
    @Override
    public int getCount() {
        return mHistroyList == null ? 0 : mHistroyList.size();
    }

    @Override
    public Object getItem(int position) {
        return null == mHistroyList ? "" : mHistroyList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (null == convertView) {
            convertView = mInflater.inflate(R.layout.history_item, parent, false);
            mViewHolder=new ViewHolder();
            mViewHolder.mTvHistroy= (TextView) convertView.findViewById(R.id.tv_history);
            mViewHolder.mIvHistroyClose= (ImageView) convertView.findViewById(R.id.iv_close);
            mViewHolder.mIvHistroyClose.setOnClickListener(this);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder= (ViewHolder) convertView.getTag();
        }
        mViewHolder.mTvHistroy.setText(mHistroyList.get(position));
        mViewHolder.mIvHistroyClose.setTag(position);
        mViewHolder.mIvHistroyClose.setVisibility(isHistory?View.VISIBLE:View.GONE);
        TextViewUtils.setTextViewDrable(mContext,isHistory?R.mipmap.icon_time:R.mipmap.icon_jieguo, mViewHolder.mTvHistroy,TextViewUtils.S_LEFT);
        return convertView;
    }

    @Override
    public void onClick(View v) {
        int pos= (int) v.getTag();
        int i = v.getId();
        if (i == R.id.iv_close) {
            mHistroyList.remove(pos);
            notifyDataSetChanged();
        }

    }
    static class ViewHolder{
        TextView mTvHistroy;//搜索内容
        ImageView mIvHistroyClose;//删除
    }
}
